
function romanToInt(s) {
  const obj = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
  };
  
  let ans = 0;
  
  for (let i = 0; i < s.length; i++) {
    let f = s[i];
    let e = s[i + 1];
    if (obj[f] < obj[e]) {
      ans -= obj[f];
    }
    else {
      ans += obj[f];
    }
  }

  return ans;
}


console.log(romanToInt('MCXCIV'));
console.log(romanToInt('MCDXXXVII'));

